package test_pais.www.parser;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.Reader;

import junit.framework.Assert;
import junit.framework.JUnit4TestAdapter;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import pais.robot.keyextr.KeywordExtractor;
import pais.robot.keyextr.KeywordListener;
import pais.robot.keyextr.SimpleKeywordExtractor;
import pais.robot.parser.HtmlParser;
import pais.robot.parser.ParseException;
import pais.robot.parser.TextListener;
import pais.robot.parser.TitleListener;
import pais.robot.parser.URLListener;

/**
 * Tests if the HtmlParser works correctly
 * @author kmkim
 *
 */
public class Test_Parser {
  public static junit.framework.Test suite() {
    return new JUnit4TestAdapter( Test_Parser.class );
  }
  @BeforeClass public static void setUpOnce() {
  }

  @Before public void setUp() {
  }

  @After public void tearDown() {
  }

  public class HtmlListener implements TextListener, URLListener, KeywordListener, TitleListener {
    private long srcUID_;
//  KeywordExtractor keyExt_ = new KoreanKeywordExtractor();
    KeywordExtractor keyExt_ = new SimpleKeywordExtractor();

    public HtmlListener() {
    }

    public void setPageUID(long srcUID) {
      srcUID_ = srcUID;
    }
    /*
     *  insert into keyword index
     *  @param uid - the URL identifier
     *  @param key - the keyword
     */
    public void onText(String text, int offset) {
      if ( text != null ) {
        if ( ! text.equals("") ) {
          System.err.println("onText("+text+") on UID("+srcUID_+")");
        }
      }
    }
    public void onTitle(String title) {
      if ( title != null ) {
        if ( ! title.equals("") ) {
          System.err.println("onTitle("+title+") on UID("+srcUID_+")");
        }
      }
    }

    public void onKeyword(String keyword, int offset) {
      System.out.println("onKeyword("+keyword+") on UID("+srcUID_+")");
    }
    public void onURL(String URL) {
      System.out.println("onURL("+URL+") from UID("+srcUID_+")");
    }
  }
  public static final String TEST_HTML_FILE="src/test/docrobot/parser/www.naver.com.html";
  private HtmlParser htmlParser_ = null;
  @Test public void testParser() {
    try {
      HtmlListener lisetner = new HtmlListener();
      lisetner.setPageUID(1);

      Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(TEST_HTML_FILE)));
      /* PAIS_BUG: Set charset */
      if ( htmlParser_ == null ) {
        htmlParser_ = new HtmlParser( reader );
      } else {
        htmlParser_.ReInit(reader);
      }

      htmlParser_.textListener_ = lisetner;
      htmlParser_.urlListener_ = lisetner;
      htmlParser_.titleListener_ = lisetner;
      try {
        htmlParser_.html();
      } catch (ParseException e) {
        // PAIS_BUG : Log error.
        System.err.println("Error while parsing HTML document");
        e.printStackTrace();
      }

    } catch (FileNotFoundException e) {
      System.err.println("HTML File Not Found ; "+ TEST_HTML_FILE);
      e.printStackTrace();
    }



    Assert.assertTrue( true );
  }

}
